import { http } from "@/utils/http";
import { CommonResponse } from "./common";

/**
 * 题型type枚举值
 */
export const QuestionType = {
  1: '单选题',
  2: '多选题',
  3: '填空题',
}

/**
 * 试题返回数据格式
 */
export type Question = {
  records: {
    id: number;
    questionNo: string;
    type: string;
    title: string;
    createBy: string;
    gmtCreate: string;
  }[]
  total: number;
  pageNum: number;
  pageSize: number;
}

/**
 * 试题请求参数
 */
export type QuestionParams = {
  type?: string;
  questionNo?: string;
  pageNum: number;
  pageSize: number;
}

/**
 * 分页查询试题
 */
export function getQuestions(data: QuestionParams) {
  return http.request<CommonResponse<Question>>(
    "post",
    "/api/platform/question/list",
    { data }
  );
}

/**
 * 创建试题请求参数
 */
export type CreateQuestionParams = {
  type: string;
  title: string;
  titleImage?: string;
  options: {
    key: string;
    content: string;
    image?: string;
  }[];
  correctAnswer: string;
}

/**
 * 创建试题
 */
export function createQuestion(data: CreateQuestionParams) {
  return http.request<CommonResponse<number>>(
    "post",
    "/api/platform/question/create",
    { data }
  );
}

/**
 * 更新试题
 */
export function updateQuestion(questionId: number, data: CreateQuestionParams) {
  return http.request<CommonResponse<number>>(
    "post",
    `/api/platform/question/${questionId}/update`,
    { data }
  );
}

/**
 * 获取题目详情
 */
export function getQuestionDetail(questionId: number) {
  return http.request<CommonResponse<any>>(
    "get",
    `/api/platform/question/${questionId}`
  );
}

/**
 * 更新试题状态
 */
export function updateQuestionStatus(questionId: number, status: number) {
  return http.request<CommonResponse<number>>(
    "post",
    `/api/platform/question/${questionId}/status/${status}`
  );
}